{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. [获取DataFrame各组件内容](#%E8%8E%B7%E5%8F%96DataFrame%E5%90%84%E7%BB%84%E4%BB%B6%E5%86%85%E5%AE%B9)\n",
    "2. [理解数据类型](#%E7%90%86%E8%A7%A3%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B)\n",
    "3. [按列选取数据](#%E6%8C%89%E5%88%97%E9%80%89%E5%8F%96%E6%95%B0%E6%8D%AE)\n",
    "4. [调用Series的方法](#%E8%B0%83%E7%94%A8Series%E7%9A%84%E6%96%B9%E6%B3%95)\n",
    "5. [Series的操作符](#Series%E7%9A%84%E6%93%8D%E4%BD%9C%E7%AC%A6)\n",
    "6. [Series方法的链式调用](#Series%E6%96%B9%E6%B3%95%E7%9A%84%E9%93%BE%E5%BC%8F%E8%B0%83%E7%94%A8)\n",
    "7. [让索引变得更有意义](#%E8%AE%A9%E7%B4%A2%E5%BC%95%E5%8F%98%E5%BE%97%E6%9B%B4%E6%9C%89%E6%84%8F%E4%B9%89)\n",
    "8. [重命名行与列](#%E9%87%8D%E5%91%BD%E5%90%8D%E8%A1%8C%E4%B8%8E%E5%88%97)\n",
    "9. [创建与删除列](#%E5%88%9B%E5%BB%BA%E4%B8%8E%E5%88%A0%E9%99%A4%E5%88%97)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np # 引用numpy包，并用缩写np代替。\n",
    "import pandas as pd # 引用pandas包，并用缩写pd代替。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.set_option('max_columns', 8, 'max_rows', 10) # 最多显示10行8列，多的用'...'表示。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>color</th>\n",
       "      <th>director_name</th>\n",
       "      <th>num_critic_for_reviews</th>\n",
       "      <th>duration</th>\n",
       "      <th>...</th>\n",
       "      <th>actor_2_facebook_likes</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>aspect_ratio</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Color</td>\n",
       "      <td>James Cameron</td>\n",
       "      <td>723.0</td>\n",
       "      <td>178.0</td>\n",
       "      <td>...</td>\n",
       "      <td>936.0</td>\n",
       "      <td>7.9</td>\n",
       "      <td>1.78</td>\n",
       "      <td>33000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Color</td>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>302.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>7.1</td>\n",
       "      <td>2.35</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Color</td>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>602.0</td>\n",
       "      <td>148.0</td>\n",
       "      <td>...</td>\n",
       "      <td>393.0</td>\n",
       "      <td>6.8</td>\n",
       "      <td>2.35</td>\n",
       "      <td>85000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Color</td>\n",
       "      <td>Christopher Nolan</td>\n",
       "      <td>813.0</td>\n",
       "      <td>164.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23000.0</td>\n",
       "      <td>8.5</td>\n",
       "      <td>2.35</td>\n",
       "      <td>164000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>12.0</td>\n",
       "      <td>7.1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   color      director_name  num_critic_for_reviews  duration  ...  \\\n",
       "0  Color      James Cameron                   723.0     178.0  ...   \n",
       "1  Color     Gore Verbinski                   302.0     169.0  ...   \n",
       "2  Color         Sam Mendes                   602.0     148.0  ...   \n",
       "3  Color  Christopher Nolan                   813.0     164.0  ...   \n",
       "4    NaN        Doug Walker                     NaN       NaN  ...   \n",
       "\n",
       "   actor_2_facebook_likes  imdb_score aspect_ratio  movie_facebook_likes  \n",
       "0                   936.0         7.9         1.78                 33000  \n",
       "1                  5000.0         7.1         2.35                     0  \n",
       "2                   393.0         6.8         2.35                 85000  \n",
       "3                 23000.0         8.5         2.35                164000  \n",
       "4                    12.0         7.1          NaN                     0  \n",
       "\n",
       "[5 rows x 28 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie = pd.read_csv('data/movie.csv')\n",
    "movie.head() # 显示前几行信息，Jupyter会自动打印最后一行返回的内容。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![DataFrame示意图](data/dataframe-anatomy.png)\n",
    "\n",
    "<b>没看明白关系也不大，后面慢慢整就清楚了！</b>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取DataFrame各组件内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "columns = movie.columns # 获取列名\n",
    "index = movie.index\n",
    "data = movie.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.indexes.base.Index'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Index(['color', 'director_name', 'num_critic_for_reviews', 'duration',\n",
       "       'director_facebook_likes', 'actor_3_facebook_likes', 'actor_2_name',\n",
       "       'actor_1_facebook_likes', 'gross', 'genres', 'actor_1_name',\n",
       "       'movie_title', 'num_voted_users', 'cast_total_facebook_likes',\n",
       "       'actor_3_name', 'facenumber_in_poster', 'plot_keywords',\n",
       "       'movie_imdb_link', 'num_user_for_reviews', 'language', 'country',\n",
       "       'content_rating', 'budget', 'title_year', 'actor_2_facebook_likes',\n",
       "       'imdb_score', 'aspect_ratio', 'movie_facebook_likes'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(type(columns)) # columns的数据结构，本质就是一个数组。\n",
    "columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.indexes.range.RangeIndex'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=4916, step=1)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(type(index))\n",
    "index # 简单的数字索引，即行号。总共有4916行数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([['Color', 'James Cameron', 723.0, ..., 7.9, 1.78, 33000],\n",
       "       ['Color', 'Gore Verbinski', 302.0, ..., 7.1, 2.35, 0],\n",
       "       ['Color', 'Sam Mendes', 602.0, ..., 6.8, 2.35, 85000],\n",
       "       ...,\n",
       "       ['Color', 'Benjamin Roberds', 13.0, ..., 6.3, nan, 16],\n",
       "       ['Color', 'Daniel Hsia', 14.0, ..., 6.3, 2.35, 660],\n",
       "       ['Color', 'Jon Gunn', 43.0, ..., 6.6, 1.85, 456]], dtype=object)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(type(data)) # numpy.ndarray是对Python默认list的增强\n",
    "data # 二维数组，每个元素对应一行数据，元素里的每个值对应每一列的值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "issubclass(pd.RangeIndex, pd.Index) # RangeIndex是Index的一个子类，用于实现类似连续的行号。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   0,    1,    2, ..., 4913, 4914, 4915], dtype=int64)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index.values # 查看索引的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['color', 'director_name', 'num_critic_for_reviews', 'duration',\n",
       "       'director_facebook_likes', 'actor_3_facebook_likes',\n",
       "       'actor_2_name', 'actor_1_facebook_likes', 'gross', 'genres',\n",
       "       'actor_1_name', 'movie_title', 'num_voted_users',\n",
       "       'cast_total_facebook_likes', 'actor_3_name',\n",
       "       'facenumber_in_poster', 'plot_keywords', 'movie_imdb_link',\n",
       "       'num_user_for_reviews', 'language', 'country', 'content_rating',\n",
       "       'budget', 'title_year', 'actor_2_facebook_likes', 'imdb_score',\n",
       "       'aspect_ratio', 'movie_facebook_likes'], dtype=object)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "columns.values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 理解数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie = pd.read_csv('data/movie.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "color                       object\n",
       "director_name               object\n",
       "num_critic_for_reviews     float64\n",
       "duration                   float64\n",
       "director_facebook_likes    float64\n",
       "                            ...   \n",
       "title_year                 float64\n",
       "actor_2_facebook_likes     float64\n",
       "imdb_score                 float64\n",
       "aspect_ratio               float64\n",
       "movie_facebook_likes         int64\n",
       "Length: 28, dtype: object"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.dtypes # 每一列的数据类型,dtypes的类型是Series，增强的数组类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float64    13\n",
       "object     12\n",
       "int64       3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.dtypes.value_counts() # 根据数据类型分组聚合统计，书上因为pandas版本较老，使用get_dtype_counts。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 按列选取数据\n",
    "\n",
    "一般关系数据库我们都是用select按行选取数据，但是Pandas提供了类似Excel的功能，可以按列选取数据，返回一个Series结构（增强版的list）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie = pd.read_csv('data/movie.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0           James Cameron\n",
       "1          Gore Verbinski\n",
       "2              Sam Mendes\n",
       "3       Christopher Nolan\n",
       "4             Doug Walker\n",
       "              ...        \n",
       "4911          Scott Smith\n",
       "4912                  NaN\n",
       "4913     Benjamin Roberds\n",
       "4914          Daniel Hsia\n",
       "4915             Jon Gunn\n",
       "Name: director_name, Length: 4916, dtype: object"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie['director_name'] # 选取director_name这一列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0           James Cameron\n",
       "1          Gore Verbinski\n",
       "2              Sam Mendes\n",
       "3       Christopher Nolan\n",
       "4             Doug Walker\n",
       "              ...        \n",
       "4911          Scott Smith\n",
       "4912                  NaN\n",
       "4913     Benjamin Roberds\n",
       "4914          Daniel Hsia\n",
       "4915             Jon Gunn\n",
       "Name: director_name, Length: 4916, dtype: object"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.director_name # 选取director_name这一列，与上一条指令效果相同。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(movie.director_name) # 每一列数据对应的数据类型是Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'director_name'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director = movie['director_name']\n",
    "director.name # 列保存了名称属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>director_name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>James Cameron</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Gore Verbinski</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Sam Mendes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Christopher Nolan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Doug Walker</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       director_name\n",
       "0      James Cameron\n",
       "1     Gore Verbinski\n",
       "2         Sam Mendes\n",
       "3  Christopher Nolan\n",
       "4        Doug Walker"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = director.to_frame() # 把一维的Series扩展成DataFrame\n",
    "frame.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 调用Series的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "433"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s_methods = dir(pd.Series) # dir用于看一看对象有哪些属性（包括成员变量和方法，变量名带methods是因为返回的大多数是方法。）\n",
    "len(s_methods)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "430"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_methods = dir(pd.DataFrame)\n",
    "len(df_methods)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie = pd.read_csv('data/movie.csv')\n",
    "director = movie['director_name']\n",
    "actor_1_fb_likes = movie['actor_1_facebook_likes']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        James Cameron\n",
       "1       Gore Verbinski\n",
       "2           Sam Mendes\n",
       "3    Christopher Nolan\n",
       "4          Doug Walker\n",
       "Name: director_name, dtype: object"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     1000.0\n",
       "1    40000.0\n",
       "2    11000.0\n",
       "3    27000.0\n",
       "4      131.0\n",
       "Name: actor_1_facebook_likes, dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_1_fb_likes.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Steven Spielberg    26\n",
       "Woody Allen         22\n",
       "Clint Eastwood      20\n",
       "Martin Scorsese     20\n",
       "                    ..\n",
       "Jonas Elmer          1\n",
       "Benjamin Roberds     1\n",
       "Carmen Marron        1\n",
       "Dan Perri            1\n",
       "Name: director_name, Length: 2397, dtype: int64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.set_option('max_rows', 8)\n",
    "director.value_counts() # 统计每个值出现多少次，类似SQL的group by。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000.0     436\n",
       "11000.0    206\n",
       "2000.0     189\n",
       "3000.0     150\n",
       "          ... \n",
       "216.0        1\n",
       "859.0        1\n",
       "225.0        1\n",
       "334.0        1\n",
       "Name: actor_1_facebook_likes, Length: 877, dtype: int64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_1_fb_likes.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4916"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director.size # 有多少条记录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4916"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(director)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4814"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director.count() # NaN（空值）的数据会被过滤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4909"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_1_fb_likes.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "982.0"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_1_fb_likes.quantile() # 中位数，不是平均数！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.0, 640000.0, 6494.488490527602, 982.0, 15106.986883848185, 31881444.0)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ','用于分开多条指令，'\\'用于换行，防止一行太长。\n",
    "# mean是平均数，median与quantile一样是中位数。\n",
    "# std是方差\n",
    "actor_1_fb_likes.min(), \\\n",
    "actor_1_fb_likes.max(), \\\n",
    "actor_1_fb_likes.mean(), \\\n",
    "actor_1_fb_likes.median(), \\\n",
    "actor_1_fb_likes.std(), \\\n",
    "actor_1_fb_likes.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count      4909.000000\n",
       "mean       6494.488491\n",
       "std       15106.986884\n",
       "min           0.000000\n",
       "25%         607.000000\n",
       "50%         982.000000\n",
       "75%       11000.000000\n",
       "max      640000.000000\n",
       "Name: actor_1_facebook_likes, dtype: float64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_1_fb_likes.describe() # 数据统计信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count                 4814\n",
       "unique                2397\n",
       "top       Steven Spielberg\n",
       "freq                    26\n",
       "Name: director_name, dtype: object"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "510.0"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_1_fb_likes.quantile(.2) # 比20%数大的数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1      240.0\n",
       "0.2      510.0\n",
       "0.3      694.0\n",
       "0.4      854.0\n",
       "        ...   \n",
       "0.6     1000.0\n",
       "0.7     8000.0\n",
       "0.8    13000.0\n",
       "0.9    18000.0\n",
       "Name: actor_1_facebook_likes, Length: 9, dtype: float64"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_1_fb_likes.quantile([.1, .2, .3, .4, .5, .6, .7, .8, .9]) # 从10%到90%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       False\n",
       "1       False\n",
       "2       False\n",
       "3       False\n",
       "        ...  \n",
       "4912     True\n",
       "4913    False\n",
       "4914    False\n",
       "4915    False\n",
       "Name: director_name, Length: 4916, dtype: bool"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director.isnull() # 判断数据是否为NaN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4916"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_1_fb_likes_filled = actor_1_fb_likes.fillna(0) # 空值填充0，并返回新列。\n",
    "actor_1_fb_likes_filled.count() # count过滤空值，因为填充，所以从4909变成4916。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4909"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_1_fb_likes_dropped = actor_1_fb_likes.dropna() # 删除空值，并返回新列。\n",
    "actor_1_fb_likes_dropped.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Steven Spielberg    0.005401\n",
       "Woody Allen         0.004570\n",
       "Clint Eastwood      0.004155\n",
       "Martin Scorsese     0.004155\n",
       "                      ...   \n",
       "Jonas Elmer         0.000208\n",
       "Benjamin Roberds    0.000208\n",
       "Carmen Marron       0.000208\n",
       "Dan Perri           0.000208\n",
       "Name: director_name, Length: 2397, dtype: float64"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director.value_counts(normalize=True) # 标准化，就是所有值的比例加起来等于1。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director.hasnans # 该列有没有空值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        True\n",
       "1        True\n",
       "2        True\n",
       "3        True\n",
       "        ...  \n",
       "4912    False\n",
       "4913     True\n",
       "4914     True\n",
       "4915     True\n",
       "Name: director_name, Length: 4916, dtype: bool"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director.notnull() # 与isnull相反，判断数据是否不为NaN。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Series的操作符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.options.display.max_rows = 6 # set_option的另一种写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n",
      "16\n",
      "True\n",
      "abcdefg\n",
      "False\n",
      "False\n",
      "{2, 3}\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for -: 'list' and 'int'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-44-e36078226f05>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m&\u001b[0m \u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# set取交集\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m-\u001b[0m \u001b[1;36m3\u001b[0m \u001b[1;31m# 类型不匹配，操作失败\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     12\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;31m# 集合set是无序的，不能用下标索引。\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'list' and 'int'"
     ]
    }
   ],
   "source": [
    "# 下面是一些关于操作符的例子\n",
    "print(5 + 9)\n",
    "print(4 ** 2) # 4 ^ 2 = 4 * 4 = 16\n",
    "a = 10 # 赋值\n",
    "print(a <= 11)\n",
    "print('abcde' + 'fg')\n",
    "print(not (5 < 9))\n",
    "print(7 in [1, 2, 3]) # 判断某个数字是否在数组里\n",
    "s = set([1, 2, 3]) & set([2, 3, 4]) # set取交集\n",
    "print(s)\n",
    "[1, 2, 3] - 3 # 类型不匹配，操作失败\n",
    "a = set([1, 2, 3])\n",
    "a[0] # 集合set是无序的，不能用下标索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       7.9\n",
       "1       7.1\n",
       "2       6.8\n",
       "       ... \n",
       "4913    6.3\n",
       "4914    6.3\n",
       "4915    6.6\n",
       "Name: imdb_score, Length: 4916, dtype: float64"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie = pd.read_csv('data/movie.csv')\n",
    "imdb_score = movie['imdb_score']\n",
    "imdb_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       8.9\n",
       "1       8.1\n",
       "2       7.8\n",
       "       ... \n",
       "4913    7.3\n",
       "4914    7.3\n",
       "4915    7.6\n",
       "Name: imdb_score, Length: 4916, dtype: float64"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imdb_score + 1 # 返回新列，每条记录分数加1。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       19.75\n",
       "1       17.75\n",
       "2       17.00\n",
       "        ...  \n",
       "4913    15.75\n",
       "4914    15.75\n",
       "4915    16.50\n",
       "Name: imdb_score, Length: 4916, dtype: float64"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imdb_score * 2.5  # 返回新列，每条记录分数乘2.5。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       1.0\n",
       "1       1.0\n",
       "2       0.0\n",
       "       ... \n",
       "4913    0.0\n",
       "4914    0.0\n",
       "4915    0.0\n",
       "Name: imdb_score, Length: 4916, dtype: float64"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imdb_score // 7 # 返回新列，每条记录分数除7（//代表除的结果取整）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        True\n",
       "1        True\n",
       "2       False\n",
       "        ...  \n",
       "4913    False\n",
       "4914    False\n",
       "4915    False\n",
       "Name: imdb_score, Length: 4916, dtype: bool"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imdb_score > 7 # 返回新列，每条记录分数是否大于7。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "director = movie['director_name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        True\n",
       "1       False\n",
       "2       False\n",
       "        ...  \n",
       "4913    False\n",
       "4914    False\n",
       "4915    False\n",
       "Name: director_name, Length: 4916, dtype: bool"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director == 'James Cameron' # 返回新列，名字是否为JC。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       19.75\n",
       "1       17.75\n",
       "2       17.00\n",
       "        ...  \n",
       "4913    15.75\n",
       "4914    15.75\n",
       "4915    16.50\n",
       "Name: imdb_score, Length: 4916, dtype: float64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imdb_score.mul(2.5) # imdb_score * 2.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       1.0\n",
       "1       1.0\n",
       "2       0.0\n",
       "       ... \n",
       "4913    0.0\n",
       "4914    0.0\n",
       "4915    0.0\n",
       "Name: imdb_score, Length: 4916, dtype: float64"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imdb_score.floordiv(7)  # imdb_score // 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        True\n",
       "1        True\n",
       "2       False\n",
       "        ...  \n",
       "4913    False\n",
       "4914    False\n",
       "4915    False\n",
       "Name: imdb_score, Length: 4916, dtype: bool"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imdb_score.gt(7) # gt = greater than, imdb_score > 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        True\n",
       "1       False\n",
       "2       False\n",
       "        ...  \n",
       "4913    False\n",
       "4914    False\n",
       "4915    False\n",
       "Name: director_name, Length: 4916, dtype: bool"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director.eq('James Cameron') # eq = equal, director == 'James Cameron'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       2\n",
       "1       2\n",
       "2       1\n",
       "       ..\n",
       "4913    1\n",
       "4914    1\n",
       "4915    1\n",
       "Name: imdb_score, Length: 4916, dtype: int32"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imdb_score.astype(int).mod(5) # astype对每个元素进行统一的类型转换，然后取除5的余数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       2\n",
       "1       2\n",
       "2       1\n",
       "       ..\n",
       "4913    1\n",
       "4914    1\n",
       "4915    1\n",
       "Name: imdb_score, Length: 4916, dtype: int32"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imdb_score.astype(int) % 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "type"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = type(1)\n",
    "type(a) # type的类型就是type，不理解的可以忽略，或者查\"Python元编程\"。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = type(imdb_score)\n",
    "s = a([1, 2, 3]) # a是Series类型，然后根据输入[1, 2, 3]构造一个Series。\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Series方法的链式调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie = pd.read_csv('data/movie.csv')\n",
    "actor_1_fb_likes = movie['actor_1_facebook_likes']\n",
    "director = movie['director_name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Steven Spielberg    26\n",
       "Woody Allen         22\n",
       "Clint Eastwood      20\n",
       "Name: director_name, dtype: int64"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director.value_counts().head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_1_fb_likes.isnull().sum() # 空值的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_1_fb_likes.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     1000\n",
       "1    40000\n",
       "2    11000\n",
       "3    27000\n",
       "4      131\n",
       "Name: actor_1_facebook_likes, dtype: int32"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 空值用0填充，然后全部转换为int类型，再显示前几条数据。\n",
    "actor_1_fb_likes.fillna(0)\\\n",
    "                .astype(int)\\\n",
    "                .head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0014239218877135883"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_1_fb_likes.isnull().mean() # 平均值，True/False对应1/0。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 让索引变得更有意义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie = pd.read_csv('data/movie.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4916, 28)"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shape = movie.shape # 行数，列数。返回的是一个元组tuple，也就是只读数组。\n",
    "shape[0], shape[1] # shape[0]行数，shape[1]列数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>color</th>\n",
       "      <th>director_name</th>\n",
       "      <th>num_critic_for_reviews</th>\n",
       "      <th>duration</th>\n",
       "      <th>...</th>\n",
       "      <th>actor_2_facebook_likes</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>aspect_ratio</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Avatar</th>\n",
       "      <td>Color</td>\n",
       "      <td>James Cameron</td>\n",
       "      <td>723.0</td>\n",
       "      <td>178.0</td>\n",
       "      <td>...</td>\n",
       "      <td>936.0</td>\n",
       "      <td>7.9</td>\n",
       "      <td>1.78</td>\n",
       "      <td>33000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pirates of the Caribbean: At World's End</th>\n",
       "      <td>Color</td>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>302.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>7.1</td>\n",
       "      <td>2.35</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Spectre</th>\n",
       "      <td>Color</td>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>602.0</td>\n",
       "      <td>148.0</td>\n",
       "      <td>...</td>\n",
       "      <td>393.0</td>\n",
       "      <td>6.8</td>\n",
       "      <td>2.35</td>\n",
       "      <td>85000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A Plague So Pleasant</th>\n",
       "      <td>Color</td>\n",
       "      <td>Benjamin Roberds</td>\n",
       "      <td>13.0</td>\n",
       "      <td>76.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai Calling</th>\n",
       "      <td>Color</td>\n",
       "      <td>Daniel Hsia</td>\n",
       "      <td>14.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>...</td>\n",
       "      <td>719.0</td>\n",
       "      <td>6.3</td>\n",
       "      <td>2.35</td>\n",
       "      <td>660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>My Date with Drew</th>\n",
       "      <td>Color</td>\n",
       "      <td>Jon Gunn</td>\n",
       "      <td>43.0</td>\n",
       "      <td>90.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23.0</td>\n",
       "      <td>6.6</td>\n",
       "      <td>1.85</td>\n",
       "      <td>456</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4916 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          color     director_name  \\\n",
       "movie_title                                                         \n",
       "Avatar                                    Color     James Cameron   \n",
       "Pirates of the Caribbean: At World's End  Color    Gore Verbinski   \n",
       "Spectre                                   Color        Sam Mendes   \n",
       "...                                         ...               ...   \n",
       "A Plague So Pleasant                      Color  Benjamin Roberds   \n",
       "Shanghai Calling                          Color       Daniel Hsia   \n",
       "My Date with Drew                         Color          Jon Gunn   \n",
       "\n",
       "                                          num_critic_for_reviews  duration  \\\n",
       "movie_title                                                                  \n",
       "Avatar                                                     723.0     178.0   \n",
       "Pirates of the Caribbean: At World's End                   302.0     169.0   \n",
       "Spectre                                                    602.0     148.0   \n",
       "...                                                          ...       ...   \n",
       "A Plague So Pleasant                                        13.0      76.0   \n",
       "Shanghai Calling                                            14.0     100.0   \n",
       "My Date with Drew                                           43.0      90.0   \n",
       "\n",
       "                                          ...  actor_2_facebook_likes  \\\n",
       "movie_title                               ...                           \n",
       "Avatar                                    ...                   936.0   \n",
       "Pirates of the Caribbean: At World's End  ...                  5000.0   \n",
       "Spectre                                   ...                   393.0   \n",
       "...                                       ...                     ...   \n",
       "A Plague So Pleasant                      ...                     0.0   \n",
       "Shanghai Calling                          ...                   719.0   \n",
       "My Date with Drew                         ...                    23.0   \n",
       "\n",
       "                                          imdb_score aspect_ratio  \\\n",
       "movie_title                                                         \n",
       "Avatar                                           7.9         1.78   \n",
       "Pirates of the Caribbean: At World's End         7.1         2.35   \n",
       "Spectre                                          6.8         2.35   \n",
       "...                                              ...          ...   \n",
       "A Plague So Pleasant                             6.3          NaN   \n",
       "Shanghai Calling                                 6.3         2.35   \n",
       "My Date with Drew                                6.6         1.85   \n",
       "\n",
       "                                          movie_facebook_likes  \n",
       "movie_title                                                     \n",
       "Avatar                                                   33000  \n",
       "Pirates of the Caribbean: At World's End                     0  \n",
       "Spectre                                                  85000  \n",
       "...                                                        ...  \n",
       "A Plague So Pleasant                                        16  \n",
       "Shanghai Calling                                           660  \n",
       "My Date with Drew                                          456  \n",
       "\n",
       "[4916 rows x 27 columns]"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie2 = movie.set_index('movie_title') # 将movie_title列做为索引列，可以根据title去定位一行数据。\n",
    "movie2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>color</th>\n",
       "      <th>director_name</th>\n",
       "      <th>num_critic_for_reviews</th>\n",
       "      <th>duration</th>\n",
       "      <th>...</th>\n",
       "      <th>actor_2_facebook_likes</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>aspect_ratio</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Avatar</th>\n",
       "      <td>Color</td>\n",
       "      <td>James Cameron</td>\n",
       "      <td>723.0</td>\n",
       "      <td>178.0</td>\n",
       "      <td>...</td>\n",
       "      <td>936.0</td>\n",
       "      <td>7.9</td>\n",
       "      <td>1.78</td>\n",
       "      <td>33000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pirates of the Caribbean: At World's End</th>\n",
       "      <td>Color</td>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>302.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>7.1</td>\n",
       "      <td>2.35</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Spectre</th>\n",
       "      <td>Color</td>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>602.0</td>\n",
       "      <td>148.0</td>\n",
       "      <td>...</td>\n",
       "      <td>393.0</td>\n",
       "      <td>6.8</td>\n",
       "      <td>2.35</td>\n",
       "      <td>85000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A Plague So Pleasant</th>\n",
       "      <td>Color</td>\n",
       "      <td>Benjamin Roberds</td>\n",
       "      <td>13.0</td>\n",
       "      <td>76.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai Calling</th>\n",
       "      <td>Color</td>\n",
       "      <td>Daniel Hsia</td>\n",
       "      <td>14.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>...</td>\n",
       "      <td>719.0</td>\n",
       "      <td>6.3</td>\n",
       "      <td>2.35</td>\n",
       "      <td>660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>My Date with Drew</th>\n",
       "      <td>Color</td>\n",
       "      <td>Jon Gunn</td>\n",
       "      <td>43.0</td>\n",
       "      <td>90.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23.0</td>\n",
       "      <td>6.6</td>\n",
       "      <td>1.85</td>\n",
       "      <td>456</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4916 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          color     director_name  \\\n",
       "movie_title                                                         \n",
       "Avatar                                    Color     James Cameron   \n",
       "Pirates of the Caribbean: At World's End  Color    Gore Verbinski   \n",
       "Spectre                                   Color        Sam Mendes   \n",
       "...                                         ...               ...   \n",
       "A Plague So Pleasant                      Color  Benjamin Roberds   \n",
       "Shanghai Calling                          Color       Daniel Hsia   \n",
       "My Date with Drew                         Color          Jon Gunn   \n",
       "\n",
       "                                          num_critic_for_reviews  duration  \\\n",
       "movie_title                                                                  \n",
       "Avatar                                                     723.0     178.0   \n",
       "Pirates of the Caribbean: At World's End                   302.0     169.0   \n",
       "Spectre                                                    602.0     148.0   \n",
       "...                                                          ...       ...   \n",
       "A Plague So Pleasant                                        13.0      76.0   \n",
       "Shanghai Calling                                            14.0     100.0   \n",
       "My Date with Drew                                           43.0      90.0   \n",
       "\n",
       "                                          ...  actor_2_facebook_likes  \\\n",
       "movie_title                               ...                           \n",
       "Avatar                                    ...                   936.0   \n",
       "Pirates of the Caribbean: At World's End  ...                  5000.0   \n",
       "Spectre                                   ...                   393.0   \n",
       "...                                       ...                     ...   \n",
       "A Plague So Pleasant                      ...                     0.0   \n",
       "Shanghai Calling                          ...                   719.0   \n",
       "My Date with Drew                         ...                    23.0   \n",
       "\n",
       "                                          imdb_score aspect_ratio  \\\n",
       "movie_title                                                         \n",
       "Avatar                                           7.9         1.78   \n",
       "Pirates of the Caribbean: At World's End         7.1         2.35   \n",
       "Spectre                                          6.8         2.35   \n",
       "...                                              ...          ...   \n",
       "A Plague So Pleasant                             6.3          NaN   \n",
       "Shanghai Calling                                 6.3         2.35   \n",
       "My Date with Drew                                6.6         1.85   \n",
       "\n",
       "                                          movie_facebook_likes  \n",
       "movie_title                                                     \n",
       "Avatar                                                   33000  \n",
       "Pirates of the Caribbean: At World's End                     0  \n",
       "Spectre                                                  85000  \n",
       "...                                                        ...  \n",
       "A Plague So Pleasant                                        16  \n",
       "Shanghai Calling                                           660  \n",
       "My Date with Drew                                          456  \n",
       "\n",
       "[4916 rows x 27 columns]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv('data/movie.csv', index_col='movie_title') # 读取数据的时候直接指定movie_title列做索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>color</th>\n",
       "      <th>director_name</th>\n",
       "      <th>num_critic_for_reviews</th>\n",
       "      <th>...</th>\n",
       "      <th>actor_2_facebook_likes</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>aspect_ratio</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Avatar</td>\n",
       "      <td>Color</td>\n",
       "      <td>James Cameron</td>\n",
       "      <td>723.0</td>\n",
       "      <td>...</td>\n",
       "      <td>936.0</td>\n",
       "      <td>7.9</td>\n",
       "      <td>1.78</td>\n",
       "      <td>33000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>Color</td>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>302.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>7.1</td>\n",
       "      <td>2.35</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Spectre</td>\n",
       "      <td>Color</td>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>602.0</td>\n",
       "      <td>...</td>\n",
       "      <td>393.0</td>\n",
       "      <td>6.8</td>\n",
       "      <td>2.35</td>\n",
       "      <td>85000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4913</th>\n",
       "      <td>A Plague So Pleasant</td>\n",
       "      <td>Color</td>\n",
       "      <td>Benjamin Roberds</td>\n",
       "      <td>13.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4914</th>\n",
       "      <td>Shanghai Calling</td>\n",
       "      <td>Color</td>\n",
       "      <td>Daniel Hsia</td>\n",
       "      <td>14.0</td>\n",
       "      <td>...</td>\n",
       "      <td>719.0</td>\n",
       "      <td>6.3</td>\n",
       "      <td>2.35</td>\n",
       "      <td>660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4915</th>\n",
       "      <td>My Date with Drew</td>\n",
       "      <td>Color</td>\n",
       "      <td>Jon Gunn</td>\n",
       "      <td>43.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23.0</td>\n",
       "      <td>6.6</td>\n",
       "      <td>1.85</td>\n",
       "      <td>456</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4916 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   movie_title  color     director_name  \\\n",
       "0                                       Avatar  Color     James Cameron   \n",
       "1     Pirates of the Caribbean: At World's End  Color    Gore Verbinski   \n",
       "2                                      Spectre  Color        Sam Mendes   \n",
       "...                                        ...    ...               ...   \n",
       "4913                      A Plague So Pleasant  Color  Benjamin Roberds   \n",
       "4914                          Shanghai Calling  Color       Daniel Hsia   \n",
       "4915                         My Date with Drew  Color          Jon Gunn   \n",
       "\n",
       "      num_critic_for_reviews  ...  actor_2_facebook_likes  imdb_score  \\\n",
       "0                      723.0  ...                   936.0         7.9   \n",
       "1                      302.0  ...                  5000.0         7.1   \n",
       "2                      602.0  ...                   393.0         6.8   \n",
       "...                      ...  ...                     ...         ...   \n",
       "4913                    13.0  ...                     0.0         6.3   \n",
       "4914                    14.0  ...                   719.0         6.3   \n",
       "4915                    43.0  ...                    23.0         6.6   \n",
       "\n",
       "      aspect_ratio movie_facebook_likes  \n",
       "0             1.78                33000  \n",
       "1             2.35                    0  \n",
       "2             2.35                85000  \n",
       "...            ...                  ...  \n",
       "4913           NaN                   16  \n",
       "4914          2.35                  660  \n",
       "4915          1.85                  456  \n",
       "\n",
       "[4916 rows x 28 columns]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie2.reset_index() # 把索引列放回普通列，变回数字序列索引（行号）。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 重命名行与列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie = pd.read_csv('data/movie.csv', index_col='movie_title')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "idx_rename = {'Avatar':'Ratava', 'Spectre': 'Ertceps'} # 字典的key是原来的名字，value是新的名字\n",
    "col_rename = {'director_name':'Director Name', \n",
    "              'num_critic_for_reviews': 'Critical Reviews'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>color</th>\n",
       "      <th>Director Name</th>\n",
       "      <th>Critical Reviews</th>\n",
       "      <th>duration</th>\n",
       "      <th>...</th>\n",
       "      <th>actor_2_facebook_likes</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>aspect_ratio</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Ratava</th>\n",
       "      <td>Color</td>\n",
       "      <td>James Cameron</td>\n",
       "      <td>723.0</td>\n",
       "      <td>178.0</td>\n",
       "      <td>...</td>\n",
       "      <td>936.0</td>\n",
       "      <td>7.9</td>\n",
       "      <td>1.78</td>\n",
       "      <td>33000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pirates of the Caribbean: At World's End</th>\n",
       "      <td>Color</td>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>302.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>7.1</td>\n",
       "      <td>2.35</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ertceps</th>\n",
       "      <td>Color</td>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>602.0</td>\n",
       "      <td>148.0</td>\n",
       "      <td>...</td>\n",
       "      <td>393.0</td>\n",
       "      <td>6.8</td>\n",
       "      <td>2.35</td>\n",
       "      <td>85000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Dark Knight Rises</th>\n",
       "      <td>Color</td>\n",
       "      <td>Christopher Nolan</td>\n",
       "      <td>813.0</td>\n",
       "      <td>164.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23000.0</td>\n",
       "      <td>8.5</td>\n",
       "      <td>2.35</td>\n",
       "      <td>164000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Star Wars: Episode VII - The Force Awakens</th>\n",
       "      <td>NaN</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>12.0</td>\n",
       "      <td>7.1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                            color      Director Name  \\\n",
       "movie_title                                                            \n",
       "Ratava                                      Color      James Cameron   \n",
       "Pirates of the Caribbean: At World's End    Color     Gore Verbinski   \n",
       "Ertceps                                     Color         Sam Mendes   \n",
       "The Dark Knight Rises                       Color  Christopher Nolan   \n",
       "Star Wars: Episode VII - The Force Awakens    NaN        Doug Walker   \n",
       "\n",
       "                                            Critical Reviews  duration  ...  \\\n",
       "movie_title                                                             ...   \n",
       "Ratava                                                 723.0     178.0  ...   \n",
       "Pirates of the Caribbean: At World's End               302.0     169.0  ...   \n",
       "Ertceps                                                602.0     148.0  ...   \n",
       "The Dark Knight Rises                                  813.0     164.0  ...   \n",
       "Star Wars: Episode VII - The Force Awakens               NaN       NaN  ...   \n",
       "\n",
       "                                            actor_2_facebook_likes  \\\n",
       "movie_title                                                          \n",
       "Ratava                                                       936.0   \n",
       "Pirates of the Caribbean: At World's End                    5000.0   \n",
       "Ertceps                                                      393.0   \n",
       "The Dark Knight Rises                                      23000.0   \n",
       "Star Wars: Episode VII - The Force Awakens                    12.0   \n",
       "\n",
       "                                            imdb_score aspect_ratio  \\\n",
       "movie_title                                                           \n",
       "Ratava                                             7.9         1.78   \n",
       "Pirates of the Caribbean: At World's End           7.1         2.35   \n",
       "Ertceps                                            6.8         2.35   \n",
       "The Dark Knight Rises                              8.5         2.35   \n",
       "Star Wars: Episode VII - The Force Awakens         7.1          NaN   \n",
       "\n",
       "                                            movie_facebook_likes  \n",
       "movie_title                                                       \n",
       "Ratava                                                     33000  \n",
       "Pirates of the Caribbean: At World's End                       0  \n",
       "Ertceps                                                    85000  \n",
       "The Dark Knight Rises                                     164000  \n",
       "Star Wars: Episode VII - The Force Awakens                     0  \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.rename(index=idx_rename, columns=col_rename).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie = pd.read_csv('data/movie.csv', index_col='movie_title')\n",
    "index = movie.index\n",
    "columns = movie.columns\n",
    "\n",
    "# tolist变成Python普通的list\n",
    "index_list = index.tolist()\n",
    "column_list = columns.tolist()\n",
    "\n",
    "# 修改索引与列的名字，注意，这些修改还没有设置回去！\n",
    "index_list[0] = 'Ratava' # 修改索引列第0个元素的值\n",
    "index_list[2] = 'Ertceps' # 修改索引列第1个元素的值\n",
    "column_list[1] = 'Director Name' # 第1列的名字修改\n",
    "column_list[2] = 'Critical Reviews' # 第2列的名字修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Ratava',\n",
       " \"Pirates of the Caribbean: At World's End\",\n",
       " 'Ertceps',\n",
       " 'The Dark Knight Rises',\n",
       " 'Star Wars: Episode VII - The Force Awakens']"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index_list[:5] # 索引前5个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['color',\n",
       " 'Director Name',\n",
       " 'Critical Reviews',\n",
       " 'duration',\n",
       " 'director_facebook_likes',\n",
       " 'actor_3_facebook_likes',\n",
       " 'actor_2_name',\n",
       " 'actor_1_facebook_likes',\n",
       " 'gross',\n",
       " 'genres',\n",
       " 'actor_1_name',\n",
       " 'num_voted_users',\n",
       " 'cast_total_facebook_likes',\n",
       " 'actor_3_name',\n",
       " 'facenumber_in_poster',\n",
       " 'plot_keywords',\n",
       " 'movie_imdb_link',\n",
       " 'num_user_for_reviews',\n",
       " 'language',\n",
       " 'country',\n",
       " 'content_rating',\n",
       " 'budget',\n",
       " 'title_year',\n",
       " 'actor_2_facebook_likes',\n",
       " 'imdb_score',\n",
       " 'aspect_ratio',\n",
       " 'movie_facebook_likes']"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "column_list # 列名，修改后的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>color</th>\n",
       "      <th>Director Name</th>\n",
       "      <th>Critical Reviews</th>\n",
       "      <th>duration</th>\n",
       "      <th>...</th>\n",
       "      <th>actor_2_facebook_likes</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>aspect_ratio</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Ratava</th>\n",
       "      <td>Color</td>\n",
       "      <td>James Cameron</td>\n",
       "      <td>723.0</td>\n",
       "      <td>178.0</td>\n",
       "      <td>...</td>\n",
       "      <td>936.0</td>\n",
       "      <td>7.9</td>\n",
       "      <td>1.78</td>\n",
       "      <td>33000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pirates of the Caribbean: At World's End</th>\n",
       "      <td>Color</td>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>302.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>7.1</td>\n",
       "      <td>2.35</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ertceps</th>\n",
       "      <td>Color</td>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>602.0</td>\n",
       "      <td>148.0</td>\n",
       "      <td>...</td>\n",
       "      <td>393.0</td>\n",
       "      <td>6.8</td>\n",
       "      <td>2.35</td>\n",
       "      <td>85000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Dark Knight Rises</th>\n",
       "      <td>Color</td>\n",
       "      <td>Christopher Nolan</td>\n",
       "      <td>813.0</td>\n",
       "      <td>164.0</td>\n",
       "      <td>...</td>\n",
       "      <td>23000.0</td>\n",
       "      <td>8.5</td>\n",
       "      <td>2.35</td>\n",
       "      <td>164000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Star Wars: Episode VII - The Force Awakens</th>\n",
       "      <td>NaN</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>12.0</td>\n",
       "      <td>7.1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                            color      Director Name  \\\n",
       "Ratava                                      Color      James Cameron   \n",
       "Pirates of the Caribbean: At World's End    Color     Gore Verbinski   \n",
       "Ertceps                                     Color         Sam Mendes   \n",
       "The Dark Knight Rises                       Color  Christopher Nolan   \n",
       "Star Wars: Episode VII - The Force Awakens    NaN        Doug Walker   \n",
       "\n",
       "                                            Critical Reviews  duration  ...  \\\n",
       "Ratava                                                 723.0     178.0  ...   \n",
       "Pirates of the Caribbean: At World's End               302.0     169.0  ...   \n",
       "Ertceps                                                602.0     148.0  ...   \n",
       "The Dark Knight Rises                                  813.0     164.0  ...   \n",
       "Star Wars: Episode VII - The Force Awakens               NaN       NaN  ...   \n",
       "\n",
       "                                            actor_2_facebook_likes  \\\n",
       "Ratava                                                       936.0   \n",
       "Pirates of the Caribbean: At World's End                    5000.0   \n",
       "Ertceps                                                      393.0   \n",
       "The Dark Knight Rises                                      23000.0   \n",
       "Star Wars: Episode VII - The Force Awakens                    12.0   \n",
       "\n",
       "                                            imdb_score aspect_ratio  \\\n",
       "Ratava                                             7.9         1.78   \n",
       "Pirates of the Caribbean: At World's End           7.1         2.35   \n",
       "Ertceps                                            6.8         2.35   \n",
       "The Dark Knight Rises                              8.5         2.35   \n",
       "Star Wars: Episode VII - The Force Awakens         7.1          NaN   \n",
       "\n",
       "                                            movie_facebook_likes  \n",
       "Ratava                                                     33000  \n",
       "Pirates of the Caribbean: At World's End                       0  \n",
       "Ertceps                                                    85000  \n",
       "The Dark Knight Rises                                     164000  \n",
       "Star Wars: Episode VII - The Force Awakens                     0  \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.index = index_list # 将新的索引与列名设置回DataFrame\n",
    "movie.columns = column_list\n",
    "movie.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建与删除列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie = pd.read_csv('data/movie.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie['has_seen'] = 0 # 新增一列，值全部设置为0。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['color', 'director_name', 'num_critic_for_reviews', 'duration',\n",
       "       'director_facebook_likes', 'actor_3_facebook_likes', 'actor_2_name',\n",
       "       'actor_1_facebook_likes', 'gross', 'genres', 'actor_1_name',\n",
       "       'movie_title', 'num_voted_users', 'cast_total_facebook_likes',\n",
       "       'actor_3_name', 'facenumber_in_poster', 'plot_keywords',\n",
       "       'movie_imdb_link', 'num_user_for_reviews', 'language', 'country',\n",
       "       'content_rating', 'budget', 'title_year', 'actor_2_facebook_likes',\n",
       "       'imdb_score', 'aspect_ratio', 'movie_facebook_likes', 'has_seen'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.columns # 跟之前的列相比，可以看到新增一列。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将不同列的值合成一个新列\n",
    "# 这里要有向量操作的概念，加法就是对应的每个元素相加，比如[1, 2] + [3, 4] = [1 + 3, 2 + 4] = [4, 6]\n",
    "movie['actor_director_facebook_likes'] = (movie['actor_1_facebook_likes'] + \n",
    "                                              movie['actor_2_facebook_likes'] + \n",
    "                                              movie['actor_3_facebook_likes'] + \n",
    "                                              movie['director_facebook_likes'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "122"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie['actor_director_facebook_likes'].isnull().sum() # 空值的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie['actor_director_facebook_likes'] = movie['actor_director_facebook_likes'].fillna(0) # 空值填充0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie['is_cast_likes_more'] = (movie['cast_total_facebook_likes'] >= \n",
    "                                  movie['actor_director_facebook_likes']) # 根据比较结果决定新列的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie['is_cast_likes_more'].all() # 是否所有元素为True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie = movie.drop('actor_director_facebook_likes', axis='columns') # 删除指定列，注意axis参数，如果是columns就是按列操作！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie['actor_total_facebook_likes'] = (movie['actor_1_facebook_likes'] + \n",
    "                                       movie['actor_2_facebook_likes'] + \n",
    "                                       movie['actor_3_facebook_likes'])\n",
    "movie['actor_total_facebook_likes'] = movie['actor_total_facebook_likes'].fillna(0) # 空值填充0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie['is_cast_likes_more'] = movie['cast_total_facebook_likes'] >= \\\n",
    "                                  movie['actor_total_facebook_likes']    \n",
    "movie['is_cast_likes_more'].all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie['pct_actor_cast_like'] = (movie['actor_total_facebook_likes'] / \n",
    "                                movie['cast_total_facebook_likes'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.0, 1.0)"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie['pct_actor_cast_like'].min(), movie['pct_actor_cast_like'].max() # 最小值，最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "movie_title\n",
       "Avatar                                        0.577369\n",
       "Pirates of the Caribbean: At World's End      0.951396\n",
       "Spectre                                       0.987521\n",
       "The Dark Knight Rises                         0.683783\n",
       "Star Wars: Episode VII - The Force Awakens    0.000000\n",
       "Name: pct_actor_cast_like, dtype: float64"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用movie_title做索引，选择某一列数据输出。这样可以来比较方便，不然没有索引不知道数据之间的关系。\n",
    "movie.set_index('movie_title')['pct_actor_cast_like'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "profit_index = movie.columns.get_loc('gross') + 1 # get_loc是获得'gross'在第几列，加1是因为索引从0开始。\n",
    "profit_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在第9列，也就是gross后面插一列，值就是gross - budget。\n",
    "movie.insert(loc=profit_index,\n",
    "                 column='profit',\n",
    "                 value=movie['gross'] - movie['budget'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['color', 'director_name', 'num_critic_for_reviews', 'duration',\n",
       "       'director_facebook_likes', 'actor_3_facebook_likes', 'actor_2_name',\n",
       "       'actor_1_facebook_likes', 'gross', 'profit', 'genres', 'actor_1_name',\n",
       "       'movie_title', 'num_voted_users', 'cast_total_facebook_likes',\n",
       "       'actor_3_name', 'facenumber_in_poster', 'plot_keywords',\n",
       "       'movie_imdb_link', 'num_user_for_reviews', 'language', 'country',\n",
       "       'content_rating', 'budget', 'title_year', 'actor_2_facebook_likes',\n",
       "       'imdb_score', 'aspect_ratio', 'movie_facebook_likes', 'has_seen',\n",
       "       'is_cast_likes_more', 'actor_total_facebook_likes',\n",
       "       'pct_actor_cast_like'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.columns # 注意gross后面是profit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>profit</th>\n",
       "      <th>gross</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>523505847.0</td>\n",
       "      <td>760505847.0</td>\n",
       "      <td>237000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9404152.0</td>\n",
       "      <td>309404152.0</td>\n",
       "      <td>300000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-44925825.0</td>\n",
       "      <td>200074175.0</td>\n",
       "      <td>245000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>198130642.0</td>\n",
       "      <td>448130642.0</td>\n",
       "      <td>250000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        profit        gross       budget\n",
       "0  523505847.0  760505847.0  237000000.0\n",
       "1    9404152.0  309404152.0  300000000.0\n",
       "2  -44925825.0  200074175.0  245000000.0\n",
       "3  198130642.0  448130642.0  250000000.0\n",
       "4          NaN          NaN          NaN"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie[['profit', 'gross', 'budget']].head() # 选取3列打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>profit</th>\n",
       "      <th>gross</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Avatar</th>\n",
       "      <td>523505847.0</td>\n",
       "      <td>760505847.0</td>\n",
       "      <td>237000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pirates of the Caribbean: At World's End</th>\n",
       "      <td>9404152.0</td>\n",
       "      <td>309404152.0</td>\n",
       "      <td>300000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Spectre</th>\n",
       "      <td>-44925825.0</td>\n",
       "      <td>200074175.0</td>\n",
       "      <td>245000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Dark Knight Rises</th>\n",
       "      <td>198130642.0</td>\n",
       "      <td>448130642.0</td>\n",
       "      <td>250000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Star Wars: Episode VII - The Force Awakens</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 profit        gross  \\\n",
       "movie_title                                                            \n",
       "Avatar                                      523505847.0  760505847.0   \n",
       "Pirates of the Caribbean: At World's End      9404152.0  309404152.0   \n",
       "Spectre                                     -44925825.0  200074175.0   \n",
       "The Dark Knight Rises                       198130642.0  448130642.0   \n",
       "Star Wars: Episode VII - The Force Awakens          NaN          NaN   \n",
       "\n",
       "                                                 budget  \n",
       "movie_title                                              \n",
       "Avatar                                      237000000.0  \n",
       "Pirates of the Caribbean: At World's End    300000000.0  \n",
       "Spectre                                     245000000.0  \n",
       "The Dark Knight Rises                       250000000.0  \n",
       "Star Wars: Episode VII - The Force Awakens          NaN  "
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.set_index('movie_title')[['profit', 'gross', 'budget']].head() # 用movie_title做索引"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
